<!DOCTYPE HTML>
<html>

<head>
    <title>proton.js-render-webglrender</title>
    <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta charset="utf-8">
    <style type="text/css">
        body {
            background-color: #333333;
            margin: 0px;
            overflow: hidden;
        }
        
        #container {
            width:100%;
            height:100%;
        }
        
        #testCanvas {
            background: #000;
        }
    </style>
</head>

<body>
    <div id="container">
        <canvas id="testCanvas"></canvas>
    </div>
    <script src="../../lib/requestAnimationFrame.min.js"></script>
    <script src="../../lib/stats.min.js"></script>
    <script src="//localhost:3001/build/proton.js"></script>
    <script>
        var proton;
        var renderer;
        var emitter;
        var stats;
        var canvas;
        var context;
        var _mousedown = false;

        main();

        function main() {
            initCanvas();
            addStats();
            createProton();
            tick();
        }

        function initCanvas(){
            canvas = document.getElementById("testCanvas");
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;

            canvas.addEventListener('mousemove', mousemoveHandler, false);
            canvas.addEventListener('mousedown', mousedownHandler, false);
            canvas.addEventListener('mouseup', mouseupHandler, false);
        }

        function addStats() {
            stats = new Stats();
            stats.setMode(2);
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';
            document.getElementById('container').appendChild(stats.domElement);
        }

        function createProton() {
            proton = new Proton;
            createImageEmitter();

            renderer = new Proton.WebGLRenderer(canvas);
            renderer.blendFunc("SRC_ALPHA", "ONE");
            proton.addRenderer(renderer);
        }

        function createImageEmitter() {
            emitter = new Proton.Emitter();
            emitter.rate = new Proton.Rate(new Proton.Span(5, 10), new Proton.Span(.01, .015));

            emitter.addInitialize(new Proton.Mass(1));
            emitter.addInitialize(new Proton.Life(1, 2));
            emitter.addInitialize(new Proton.Body(['image/particle.png'], 32));
            emitter.addInitialize(new Proton.Radius(40));
            emitter.addInitialize(new Proton.V(new Proton.Span(1, 3), 65, 'polar'));

            emitter.addBehaviour(new Proton.Alpha(1, 0));
            emitter.addBehaviour(new Proton.Color('#4F1500', '#0029FF'));
            emitter.addBehaviour(new Proton.Scale(3, 0.1));
            emitter.addBehaviour(new Proton.CrossZone(new Proton.RectZone(0, 0, canvas.width, canvas.height), 'dead'));

            emitter.p.x = canvas.width / 2;
            emitter.p.y = canvas.height / 2;
            emitter.emit();
            proton.addEmitter(emitter);

            proton.stats.add(2, document.getElementById('container'));
        }

        function mousedownHandler(e) {
            _mousedown = true;
        }

        function mousemoveHandler(e) {
            if (_mousedown) {
                if (e.layerX || e.layerX == 0) {
                    emitter.p.x = e.layerX;
                    emitter.p.y = e.layerY;
                } else if (e.offsetX || e.offsetX == 0) {
                    emitter.p.x = e.offsetX;
                    emitter.p.y = e.offsetY;
                }
            }
        }

        function mouseupHandler(e) {
            _mousedown = false;
        }

        function tick() {
            requestAnimationFrame(tick);

            stats.begin();
            proton.update();
            proton.stats.update(3);
            stats.end();
        }
    </script>
</body>

</html>